const city = require("./mock/city.json");
const { list } = require("./mock/index");
const bodyParser = require("body-parser");
module.exports = (app) => {
  app.use(bodyParser.json());
  app.get("/api/city", (req, res) => res.send({ code: 200, data: city }));
  app.get("/api/list", (req, res) => {
    const { value = "" } = req.query;

    res.send({
      code: 200,
      data: list.filter(
        (v) => v.title.includes(value) || v.desc.includes(value)
      ),
    });
  });
  app.get("/api/detail/:id", (req, res) => {
    const { id } = req.params;
    const result = list.find((v) => v.id === id);
    res.send({
      code: 200,
      data: result,
    });
  });

  app.post("/api/login", (req, res) => {
    const { username, password } = req.body;
    if (username === "admin" && password === "admin") {
      res.send({
        code: 200,
        message: '登陆成功',
        data: {
          token: 'jeaskr;felAMW;DAKWWERWEWKNED;WIQW' + Math.random(),
          username: '管理员'
        }
      })
    } else{
      res.send({
        code: 401,
        message: '失败'
      })
    }
  });
};
